﻿using System.Security.AccessControl;
using System.Security.Principal;

namespace _15_目录;

internal class Program
{
    private static void Main(string[] args)
    {
        #region 1_创建目录

        //var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        //var dirName = @$"{docPath}\test";
        //DirectoryInfo di = Directory.CreateDirectory(dirName);
        //Console.WriteLine(di.FullName);
        //if(Directory.Exists(dirName))
        //{
        //    Console.WriteLine("目录已存在");
        //}
        //else
        //{
        //    Console.WriteLine("目录不存在");
        //}

        #endregion

        #region 2_获取当前目录

        //var curDir = Directory.GetCurrentDirectory();
        //Console.WriteLine(curDir);
        //Console.WriteLine(Directory.GetDirectoryRoot(curDir));

        #endregion

        #region 3_删除目录

        //var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        //var myDir = $@"{docPath}/test3";

        //Directory.CreateDirectory(myDir);
        //Console.WriteLine(Directory.Exists(myDir));
        //Directory.Delete(myDir);
        //Console.WriteLine(Directory.Exists(myDir));

        #endregion

        #region 4_移动目录

        //var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        //var sourceDir = $@"{docPath}\test";
        //var destDir = $@"{docPath}\test2";
        //Directory.Move(sourceDir, destDir); // 移动目录, 源目录必须存在

        #endregion

        #region 5_列出驱动器

        //var drives = Directory.GetLogicalDrives();
        //foreach (var drive in drives)
        //{
        //    Console.WriteLine(drive);
        //}

        #endregion

        #region 6_列出目录

        //var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        //var dirs = Directory.GetDirectories(docPath);
        //Console.WriteLine(dirs.Length);
        //foreach (var dir in dirs)
        //{
        //    Console.WriteLine(dir);
        //}
        //Console.WriteLine("添加搜索条件的目录列出");
        //Console.WriteLine(docPath);

        //// 列出当前目录下的 w 开头的目录
        //string[] myDirs = Directory.GetDirectories(docPath, "w*",
        //        SearchOption.TopDirectoryOnly);

        //Console.WriteLine("Directories:");

        //foreach (var myDir in myDirs)
        //{
        //    Console.WriteLine(myDir);
        //}

        #endregion

        #region 7_列出文件

        //var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        //// 形如目录, 文件中也可以进行筛选, 将所有以 t 开头的文件列出
        //string[] files = Directory.GetFiles(docPath,"t*", SearchOption.TopDirectoryOnly);
        //foreach (var file in files)
        //{
        //    Console.WriteLine(file);
        //}

        #endregion

        #region 8_目录时间

        //var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        //string[] directories = Directory.GetDirectories(docPath);
        //foreach (var dir in directories)
        //{
        //    Console.WriteLine($"{dir} : {Directory.GetCreationTime(dir)}"); // 创建时间
        //    Console.WriteLine($"{dir} : {Directory.GetLastAccessTime(dir)}"); // 最后访问时间(查看目录也算)
        //    Console.WriteLine($"{dir} : {Directory.GetLastWriteTime(dir)}"); // 最后写入时间
        //}

        #endregion

        #region 9_列出条目(目录 + 文件)

        //var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        //string[] entries = Directory.GetFileSystemEntries(docPath, "t*", SearchOption.TopDirectoryOnly);
        //foreach (var entry in entries)
        //{
        //    Console.WriteLine(entry);
        //}

        #endregion

        #region 10_目录大小: 也就是里面的文件的大小, 使用FileInfo.Length来获取文件大小

        //var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        //long size = 0;
        //var filePath = $@"{docPath}/新建文件夹";
        //var dirInfo = new DirectoryInfo(filePath);
        //FileInfo[] fileInfos = dirInfo.GetFiles("*", SearchOption.AllDirectories);
        //foreach (var fileInfo in fileInfos)
        //{
        //    size += fileInfo.Length;
        //};
        //Console.WriteLine($"{filePath} 大小: {size} bytes!");

        #endregion

        #region 11_复制目录: 先创建一个目录, 然后将所有的文件全部复制到目标目录上去

        //var basePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        //var destDir = $@"{basePath}/dest";
        //var sourceDir =  $@"{basePath}/新建文件夹";
        //DirectoryCopy(sourceDir, destDir, true);
        //Console.WriteLine("Copying finished");

        #endregion

        #region 12_目标访问控制列表

        var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        var myDir = $@"{docPath}\test";

        var dirInfo = new DirectoryInfo(myDir);
        var dSecurity = dirInfo.GetAccessControl();
        var acl = dSecurity.GetAccessRules(true, true,
            typeof(NTAccount));

        foreach (FileSystemAccessRule ace in acl)
        {
            Console.WriteLine("Account: {0}", ace.IdentityReference.Value);
            Console.WriteLine("Type: {0}", ace.AccessControlType);
            Console.WriteLine("Rights: {0}", ace.FileSystemRights);
            Console.WriteLine("Inherited: {0}", ace.IsInherited);

            Console.WriteLine("------------------------");
        }

        #endregion
    }

    /// <summary>
    ///     复制目录
    /// </summary>
    /// <param name="sourceDirName"></param>
    /// <param name="destDirName"></param>
    /// <param name="copySubDirs"></param>
    /// <exception cref="DirectoryNotFoundException"></exception>
    public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs = true)
    {
        // Get the subdirectories for the specified directory.
        var dir = new DirectoryInfo(sourceDirName);

        if (!dir.Exists)
            throw new DirectoryNotFoundException(
                "Source directory does not exist or could not be found: "
                + sourceDirName);

        var dirs = dir.GetDirectories();
        // If the destination directory doesn't exist, create it.
        if (!Directory.Exists(destDirName)) Directory.CreateDirectory(destDirName);

        // Get the files in the directory and copy them to the new location.
        var files = dir.GetFiles();

        foreach (var file in files)
        {
            var tempPath = Path.Combine(destDirName, file.Name);
            file.CopyTo(tempPath, false);
        }

        if (copySubDirs)
            foreach (var subdir in dirs)
            {
                var tempPath = Path.Combine(destDirName, subdir.Name);
                DirectoryCopy(subdir.FullName, tempPath, copySubDirs);
            }
    }
}